package cn.zwt.wolf.service.base.tag;

import cn.zwt.wolf.domain.base.tag.Tag;
import cn.zwt.wolf.repository.base.tag.TagRepository;
import cn.zwt.wolf.util.JPQLSearchException;
import cn.zwt.wolf.util.SearchUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.persistence.NoResultException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by zhangwt on 16-3-7.
 */
@Service
public class TagService {
    @Autowired
    private TagRepository tagRepository;
    @Autowired
    private SearchUtil searchUtil;
    private Logger logger = LoggerFactory.getLogger(TagService.class);

    public Tag findTagById(int id){
        return tagRepository.findOne(id);
    }

    public Tag findTagByName(String name){
        Tag tag = null;
        if (name == null)
            return tag;
        String hql = "from Tag where status = 1 and name=:name";
        Map<String,Object> params= new HashMap<String, Object>();
        params.put("name",name);
        try {
            tag = (Tag) searchUtil.getSingleResult(hql, params);
        } catch (JPQLSearchException e) {
            logger.error("findTagByName error :" + e.getMessage());
        }catch (NoResultException e){
            logger.error("findTagByName error :" + e.getMessage());
        }
        return tag;
    }
//    /**
//     * 查询信息
//     * @param_TagDto
//     * @return
//     */
//    public Page<Tag> findTagByPage(TagDto tagDto){
//        PageRequest pageable = PageUtils.getPageRequest(tagDto);
//        Page<Tag> tagPages = null;
//        String hql = "from Tag where 1=1 ";
//        Map<String,Object> params = new HashMap<String, Object>();
//        if(tagDto.getName()!=null&&tagDto.getName().length()>0){
//            hql +=" and name like:name";
//            params.put("name", "%" + tagDto.getName() + "%");
//        }
//        if(tagDto.getCode()!=null&&tagDto.getName().length()>0){
//            hql+=" and code like:code";
//            params.put("code", "%" + tagDto.getCode() + "%");
//        }
//        hql +=" and status != -1";
//        try {
//            tagPages = searchUtil.getResultPage(hql, params, pageable);
//        } catch (JPQLSearchException e) {
//            e.printStackTrace();
//        }
//        return tagPages;
//    }
    public Tag findTagByCode(String code){
        Tag tag = null;
        if (code == null)
            return tag;
        String hql = "from Tag where status = 1 and code=:code";
        Map<String,Object> params= new HashMap<String, Object>();
        params.put("code",code);
        try {
            tag = (Tag) searchUtil.getSingleResult(hql,params);
        } catch (JPQLSearchException e) {
            logger.error("findTagByCode error :" + e.getMessage());
        }catch (NoResultException e){
            logger.error("findTagByCode error :" + e.getMessage());
        }
        return tag;
    }

    public void saveOrUpdate(Tag tag){
        if (tag != null)
            tagRepository.save(tag);
    }

//    public void deleteTag(int id){
//        Tag tag = this.findTagById(id);
//        if (tag != null){
//            tag.setStatus(GlobleConstant.STATUS_DELETE_INT);
//        }
//        this.saveOrUpdate(tag);
//    }
}